import { ITimer } from "../../api/timer/ITimer";
import { E_TimeConfig } from "../defs/com/E_TimeConfig";
import { CEHandUp } from "../excel_extend/CEHangUp";
import { Player } from "../struct/Player";


/**挂机时间节点 */
export const HANG_UP_TIMES = [
    0,
    E_TimeConfig.Hour * 1,
    E_TimeConfig.Hour * 6,
    E_TimeConfig.Hour * 12
];


/**
 * 数据-挂机
 */
export abstract class SDHandUp {


    /**
     * 挂机奖励倒计时
     * @param player 
     * @param nowTime 
     * @returns 倒计时 <=0-要请求更新
     */
    public static getHangUpRewardCountDown(
        player: Player,
        nowTime = ITimer.getServerTime()
    ): number {
        const lt = player.battleStage.hangUpRewardUpdateTime;
        const nt = lt + E_TimeConfig.Minute - nowTime;                     //常规掉落
        if (nt > 0) {
            const dt = CEHandUp.miniUpdateDuraction;
            const ut = dt - ((nowTime - lt) % dt);     // + 1时间（+到100000产生一次掉落。。。）
            return nt < ut ? nt : ut;
        }
        return 0;
    }


    /**
     * 获取宝箱状态
     * @param player 
     * @param nowTime 
     */
    public static getHandUpBoxStatus(
        player: Player,
        nowTime = ITimer.getServerTime()
    ): number {
        const bt = HANG_UP_TIMES;
        const t = this.getHangUpTime(player, nowTime);
        for (let i = bt.length - 1; i >= 0; i--) {
            if (t >= bt[i]) {
                return i;
            }
        }
        return 0;
    }


    /**
     * 获取挂机时间
     * @param player 
     */
    public static getHangUpTime(
        player: Player,
        nowTime = ITimer.getServerTime()
    ): number {
        if (player) {
            const maxt = HANG_UP_TIMES[HANG_UP_TIMES.length - 1];
            let t = nowTime - player.battleStage.hangUpRewardLastGetTime;
            if (t > maxt) {
                t = maxt;
            }
            return t;
        }
        return 0;
    }



}
